<?php
/**
 * Created by PhpStorm.
 * Author: DaXiong
 * Date: 2018/10/16
 * Time: 16:07
 */

namespace App\Http\Controllers\Applet;


use App\Http\Controllers\Controller;
use App\Models\AppletActivity;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ActivityController extends Controller
{
    /**
     * 首页活动列表 get
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function index(){
        $where['status'] = 1;
        $where['pay'] = 1;
        $info = AppletActivity::where($where)->get();
        if(!$info->isEmpty()){
            return $this->returnJson(0,'首页活动列表',$info);
        }
        return $this->returnJson(1,'暂无活动推荐',$info);
    }

    /**
     * 活动详情信息 get
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function info(Request $request){
        $id = (int)$request->get('id','');
        $info = AppletActivity::where('id',$id)->where('status',1)->get();

        if(!$info->isEmpty()){
            return $this->returnJson(0,'活动信息详情',$info);
        }
        return $this->returnJson(1,'该活动不存在或已结束！');
    }

    /**
     * 活动发布提交 post
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function activityUpdate(Request $request){
        $data = $request->all();
        $data['status'] = 1;
        try{
            $info = AppletActivity::create($data);
            if ($info){
                return $this->returnJson(0,'活动发布成功');
            }else {
                return $this->returnJson(1,'活动发布失败');
            }
        }catch (\Exception $exception){
            Log::error($exception);
            return $this->returnJson(1,'系统繁忙');
        }
    }



}